<?php

/**
 * index actions.
 *
 * @package    theconnexter
 * @subpackage index
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class indexActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request){

	//permet de determiner la culture de l'utisateur
	if (!$request->getParameter('sf_culture')){
		if ($this->getUser()->isFirstRequest()){
			$culture = $request->getPreferredCulture(array('en', 'fr'));
			$this->getUser()->setCulture($culture);
			$this->getUser()->isFirstRequest(false);
		}else{
			$culture = $this->getUser()->getCulture();
		}
		$this->redirect('@localized_homepage');
	}
	
	//journal en cours 
	$this->journalEncours=Doctrine_core::getTable('journal')->getJournalEncours();
//	//print_r($this->journalEncours->toArray()); //die();
  }
  
  //liste toute la faq du site
  public function executeFaqlist (sfWebRequest $request){
	$this->listfaq=Doctrine_core::getTable('faq')->executeFaq(0);
  }
  
  //envoi le mail au destinatiare et sauvegarde une copie dans la database
  public function executeContact (sfWebRequest $request){
	$this->form= new ContactForm();
	if($request->isMethod('post')){
		$this->processForm($request, $this->form);
	}
  }
  
  protected function processForm(sfWebRequest $request, sfForm $form){
	$form->bind($request->getParameter($form->getName()));
	if ($form->isValid()){
		//on envoi un mail a l'exp�diteur et au destinataire
		//$this->sendmail($request);
		$contact = $form->save();
		$this->redirect('@homepage');
	}
  }
  public function sendmail(sfWebRequest $request){
	$message = Swift_Message::newInstance();
	$message->setSubject($request->getParameter('objet'));
	//exp�diteur1
	$message->setFrom($request->getParameter('email'));
	//destinatiare1
	$message->setTo(sfConfig::get('app_site_emailsite'));
	//contenu de votre message
	$body="<html>
				<head></head>
				<body>
					<h4>M., Mne, Mlle,".$request->getParameter('nameprenom') ." vous a laissez un message</h4>
					<p>".$request->getParameter('resume')."</p>
				</body>
		   </html>";
	$message->setBody($body,'text/html');
	$this->getMailer->send($message);
  }
  
  //met � jour le sondage
  public function executeSondage(sfWebRequest $request){
	if($request->isMethod('post')){
		Doctrine_core::getTable('sondage')->updateVote($request->getParameter('choix'));
		$this->redirect('@homepage');
		exit();
	}
  }
  
  public function executeNewletter(sfWebRequest $request){
    $subscribeUser = new NewsletterInscrits();
    $subscribeUser->setEmail($request->getParameter('email'));
    $subscribeUser->setEtat(true);
    $subscribeUser->save();
	$this->redirect('@homepage');
 }
 
 public function executeOtherContent(sfWebRequest $request){
	$this->othercontent=Doctrine_core::getTable('AutreContenu')->findAll();
 }
 
 //fonction de recherche
 public function executeSearch(sfWebRequest $request){
	if(!$query = $request->getParameter('search')){
		return $this->forward('index', 'index');
	}
    $this->journal = Doctrine_Core::getTable('Journal') ->getForLuceneQuery($query);
 }
 
 public function executeFeed(sfWebRequest $request){
	$this->journalfeed=Doctrine_core::getTable('journal')->getJournalEncours(sfConfig::get('app_max_news_feed'));
 }
 
  //fonction de recherche
 public function executeExternrss(sfWebRequest $request){
 $rssNews = Doctrine_core::getTable('rssNews')->findAll();
 require_once(sfConfig::get('sf_lib_dir').'/vendor/magpierss/rss_fetch.inc');
 $url_feed = "http://feeds.feedburner.com/itnewsafrica?format=xml";
 $html = "";
 foreach($rssNews as $feed) {
 
	 $rss = fetch_rss($feed->getLink());		
	 // si la lecture s'est bien passee, on lit les elements 
	 if (is_array($rss->items)) 	{ 
		    $html .= "<h2 class=\"feed_title\">".$feed->getTitle() ."</h2>";
			// on ne récupère que les éléments les plus récents 
			$items = array_slice($rss->items, 0, 5); 
			
			// debut de la liste
			// (vous pouvez indiquer un style CSS pour la formater) 
			$html .= "<ul class=\"feed_list\">"; // boucle sur tous les elements foreach ($items as $item) 
			foreach ($items as $item)
			{ 
				$html .= "<li><a target=\"_blank\" href=\"".$item['link']."\">"; 
				$html .= $item['title']."</a></li>\n";
			} 
				$html .= "</ul>\n"; 
		} 
 }
  $this->html = $html;

} 



}
